<template>
  <div class="course-details-container">
    <el-container>
      <el-header class="header">
        <div class="header-content">
          <span class="header-title">
            <span style="padding-right:50px ;" v-if="classroomName">班级名称：{{ classroomName }}</span>
            <span>{{ classroomName ? '课程名称：' : '精品课：' }}</span>
            {{ courseDetail.courseName }}
          </span>
          <div class="header-actions">
            <div class="buttons">
              <!-- 待审核状态(2)，显示审核和驳回按钮 -->
              <el-button type="success" @click="approveCourse" v-if="courseDetail.status === 2">通过</el-button>
              <el-button type="danger" @click="rejectCourse" v-if="courseDetail.status === 2">驳回</el-button>
              
              <!-- 已发布状态(3)，显示不可点击的已发布 -->
              <el-button type="success" :disabled="true" v-if="courseDetail.status === 3">已发布</el-button>
              
              <!-- 驳回状态(4)，显示不可点击的已驳回 -->
              <el-button type="danger" :disabled="true" v-if="courseDetail.status === 4">已驳回</el-button>
            </div>
            <el-button class="back-button" @click="goBack" type="primary">返回</el-button>
          </div>
        </div>
      </el-header>
      <el-main class="main">
        <!-- 其他内容 -->
        <!-- 表格 -->
        <page-container style="width: 100%;">
          <el-table :data="videoList" height="94%" style="width: 100%">
            <el-table-column align="center" width="100px" label="序号">
              <template #default="{ $index }">
                {{ (params.pageNum - 1) * params.pageSize + $index + 1 }}
              </template>
            </el-table-column>
            <el-table-column align="left" prop="videoName" :show-overflow-tooltip="true" label="视频名称" />
            <!-- <el-table-column align="left" prop="videoDuration" :show-overflow-tooltip="true" label="视频时长" /> -->
            <el-table-column prop="videoDuration" label="视频时长" align="center" :formatter="formatDuration">
            </el-table-column>
            <el-table-column align="left" prop="createTime" :show-overflow-tooltip="true" label="创建时间" />
            <el-table-column label="操作" align="center" width="180px">
              <template #default="{ row }">
                <el-button type="text" plain @click="playBack(row.videoId)">
                  播放
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination background layout="total, sizes, prev, pager, next, jumper" :total="total"
            v-model:current-page="params.pageNum" v-model:page-size="params.pageSize" :page-sizes="[10, 20, 30]"
            @size-change="handleSizeChange" @current-change="handleCurrentChange"
            style="margin:20px 100px 20px 0; justify-content: flex-end" />
        </page-container>
      </el-main>

    </el-container>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from "vue";
import { useRoute } from "vue-router";
import { getCourseDetailService, reviewCourseService } from "@/apis/course";
import { getVideoListService } from "@/apis/video";
import router from "@/router";
import { ElMessage } from 'element-plus'
const formatDuration = (row, column, cellValue) => {
  // 1. 基础类型校验
  const duration = Number(cellValue ?? 0)
  if (isNaN(duration)) return '00:00'

  // 2. 时间计算
  const hours = Math.floor(duration / 3600)
  const minutes = Math.floor((duration % 3600) / 60)
  const seconds = Math.floor(duration % 60)

  // 3. 格式标准化
  const pad = n => n.toString().padStart(2, '0')
  return hours > 0
    ? `${pad(hours)}:${pad(minutes)}:${pad(seconds)}`
    : `${pad(minutes)}:${pad(seconds)}`
}
function goBack() {
  router.go(-1);
}

const courseDetail = reactive({});
let courseId = useRoute().query.courseId;
let classroomName = useRoute().query.classroomName;

async function getCourseDetail() {
  const res = await getCourseDetailService(courseId);
  Object.assign(courseDetail, res.data);
}

onMounted(() => {
  getCourseDetail();
});

const total = ref(0)
const params = reactive({
  courseId: courseId,
  pageNum: 1,
  pageSize: 10
})
const videoList = ref([])

async function getVideoList() {
  const result = await getVideoListService(params)
  videoList.value = result.rows
  total.value = result.total
}
getVideoList()

// 分页
function handleSizeChange(newSize) {
  params.pageNum = 1
  getVideoList()
}

function handleCurrentChange(newPage) {
  getVideoList()
}

function playBack(videoId) {
  router.push(`/tims/playBack?videoId=${videoId}`)
}

async function approveCourse() {
  try {
    await reviewCourseService(courseId, 3);
    ElMessage.success('通过课程审核成功');
    // 更新课程状态为已发布
    courseDetail.status = 3;
  } catch (error) {
    ElMessage.error('通过课程审核失败：' + (error.message || '未知错误'));
  }
}

async function rejectCourse() {
  try {
    await reviewCourseService(courseId, 4);
    ElMessage.success('驳回课程审核成功');
    // 更新课程状态为已驳回
    courseDetail.status = 4;
  } catch (error) {
    ElMessage.error('驳回课程审核失败：' + (error.message || '未知错误'));
  }
}
</script>

<style lang="scss" scoped>
/* 全局样式调整已移至main.scss */
.course-details-container {
  background-color: #f0f4ff;
  height: 100%;
  width: 100%;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;

  :deep(.el-container) {
    height: 100%;
    display: flex;
    flex-direction: column;
    margin: 0;
    padding: 0;
  }

  .header {
    height: 60px;
    display: flex;
    align-items: center;
    background-color: #007bff;
    color: white;
    padding: 0;
    margin: 0;
    flex-shrink: 0;

    .header-content {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 20px;
      width: 100%;

      .header-title {
        font-size: 20px;
      }

      .header-actions {
        display: flex;
        align-items: center;

        .buttons {
          display: flex;
          margin-right: 10px;

          .el-button--success {
            color: white !important;
          }
        }

        .back-button, .status-button {
          &.el-button--primary {
            color: white !important;
          }
        }
      }
    }
  }

  .main {
    display: flex;
    justify-content: center;
    padding: 10px;
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;

    .page-container {
      width: 100%;
      max-width: 1400px;
      background-color: white;
      border-radius: 8px;
      box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
      overflow: hidden;
      margin: 0;
      padding: 10px;

      .el-table {
        width: 100%;
        border: none;

        .el-table-column {
          background-color: #f9f9f9;
        }
        
        /* 移除表头的默认背景色 */
        :deep(.el-table__header-wrapper th.el-table__cell) {
          background-color: transparent !important;
          background: none !important;
        }
        
        /* 确保表头文字颜色正常 */
        :deep(.el-table__header-wrapper th.el-table__cell .cell) {
          color: #606266 !important;
        }
        
        /* 移除表头的悬停效果 */
        :deep(.el-table__header-wrapper th.el-table__cell.hover-row) {
          background-color: transparent !important;
        }
      }

      .el-pagination {
        margin: 20px 0;
        display: flex;
        justify-content: flex-end; // Align pagination to the right
        position: relative; // Ensure positioning works correctly

        .el-pagination__sizes {
          margin-right: 10px; // Add some space between size selector and pagination
        }
      }
    }
  }
}
</style>